import store from '../vuex'
import axios from 'axios'
import 'nprogress/nprogress.css'
import NProgress from 'nprogress'


import getuuid from '@/utils/uuid'
// 2. 创建axios实例对象
const ajax = axios.create({
  // baseURL: 'http://sph-h5-api.atguigu.cn',
  baseURL:'/api',
  // 设置超时时间
  timeout:5000
})
ajax.interceptors.request.use(config => {
  NProgress.start()
  config.headers.userTempId = getuuid()
  // console.log(config.headers.userTempId);
  const token = store.state.user.user.token
  if(token){
    config.headers.token = token
  }
  return config
}, error => {
  return Promise.reject(error);
})
ajax.interceptors.response.use(response => {
  NProgress.done()
  return response.data
}, error => {
  NProgress.done()
 return Promise.reject(new Error(error.message));
})
// 5. 向外导出实例对象
export default ajax